package com.googlecode.openrtp.grizzly.server;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.nio.transport.UDPNIOTransport;
import com.googlecode.openrtp.spi.annotation.Rtp;
import com.googlecode.openrtp.spi.server.Server;
import com.googlecode.openrtp.spi.server.config.Config;

public class GrizzlyRtpServer implements Server {

    private static final Logger LOG = Logger.getLogger(GrizzlyRtpServer.class.getName());
    private String host;
    private int port;
    private UDPNIOTransport transport;

    @Inject
    public GrizzlyRtpServer(
            @Named(Config.RTP_SERVER_HOST) String host,
            @Named(Config.RTP_SERVER_PORT) int port,
            @Rtp UDPNIOTransport transport) {
        this.host = host;
        this.port = port;
        this.transport = transport;

    }

    @Override
    public void start() {

        LOG.log(Level.INFO, "Starting on host {0} and port {1}", new Object[]{host, port});

        try {
            transport.bind(host, port);
            transport.start();
        } catch (IOException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void stop() {
        try {
            transport.stop();
        } catch (IOException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }
}
